<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------

namespace app\admin\controller;

use \think\Db;

class Goods extends Permissions
{
    /**
     * 骰盅列表
     * @return [type] [description]
     */
    public function index()
    {
        $post = $this->request->param();
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $where['name'] = ['like', '%' . $post['keywords'] . '%'];
        }
        if(isset($post['create_time']) and !empty($post['create_time'])) {
            $min_time = strtotime($post['create_time']);
            $max_time = $min_time + 24 * 60 * 60;
            $where['add_time'] = [['>=',$min_time],['<=',$max_time]];
        }

        $goods = empty($where) ? Db::name('goods')->order('create_time desc')->where(['style_type' => 1])
                  ->paginate(20) : Db::name('goods')->where(['style_type' => 1])->where($where)->order('create_time desc')
                  ->paginate(20,false,['query'=>$this->request->param()]);

        $this->assign('goods',$goods);
        return $this->fetch();
    }

    /**
     * 更新骰盅信息
     * @return mixed|void
     */
    public function update()
    {
        $id = input('id');
        if(!$id){
            return $this->error('缺少参数');
        }
        $info = Db::name('goods')->where('id',$id)->find();
        $asr = json_decode($info['thumb']);
        $this->assign('asr',$asr);
        $this->assign('info',$info);
        return $this->fetch();
    }
    
    /**
     * 骰盅的添加及修改
     * @return [type] [description]
     */
    public function publish()
    {
        //获取id
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if($id > 0) {
            //是修改操作
            if($this->request->isPost()) {
                /**
                 * 修改的话重新生成动态图
                 */
                $post                =   $this->request->post();
                if(!empty(array_filter(input("thumb/a")))){
                    foreach(input("thumb/a") as $v){
                        if(is_file('.' . $v)){
                            $thumb[] = $v;
                        }
                    }
                    $post['effect']      =   get_img($thumb, 'gif', 10, 'touzhong_' . md5(time()), '/uploads/gif/', false);
                    $post['thumb']       =   json_encode(input("thumb/a"));
                }

                $post['update_time'] = time();
                $list = Db::name('goods')->where('id',$id)->update($post);
                if($list != false) {
                    return $this->success('修改成功','admin/goods/index');
                } else {
                    return $this->error('修改失败');
                }
            }
        } else {
            //是新增操作
            if($this->request->isPost()) {
                /**
                 * 首先将商品信息插入，然后生成图片后更新
                 */
                $post = $this->request->post();
                if(input("thumb/a") !== null){
                    $post['thumb']          =   json_encode(input("thumb/a"));
                    /**
                     * 指定生成的图片名字和路径
                     */
                    $name   =   'touzhong_' . md5(time());
                    $path   =   '/uploads/gif/';
                    $post['effect']    =   get_img(json_decode($post['thumb']), 'gif', 10, $name, $path, false);
                }

                $post['create_time']    =   time();
                $post['update_time']    =   time();

                $insert = Db::name('goods')->insert($post);

//                $ls = Db::name('goods')->where(['id' => Db::name('goods')->getLastInsID()])->update($data);

                if(!$insert) {
                    return $this->error('添加失败');
                } else {
                    return $this->success('添加成功','admin/goods/index');
                }
            }
            return $this->fetch();
        }
    }

    /**
     * 删除
     * @return [type] [description]
     */
    public function delete()
    {
        if($this->request->isAjax()) {
            $id         =   $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            $resource   =   Db::name('goods')->where(['id' => $id])->find();
            /**
             * 同时删除图片和动态图
             */
            foreach(json_decode($resource['thumb'], true) as $v){
                @unlink('.' . $v);
            }
            @unlink('.' . $resource['effect']);
            $list       =   Db::name('goods')->where('id',$id)->delete();
            if($list){
                return $this->success('删除成功','admin/goods/index');
            }else{
                return $this->error('删除失败');
            }
        }
    }

    /**
     * 删除图片
     */
    public function deleteImage()
    {
        @unlink('.' . trim($this->request->post('image_url'), '.'));
    }

}
